﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace Palsta.Core
{
    public class XslIf : XslElement
    {
        /*
         * Constructors
         */ 
        public XslIf(XmlNode xslNode) :
            base(xslNode)
        {
            _jsFunctionName = _functionPrefix + "if"; //specify the js function name
        }

        /*
         * Overrides
         */ 
        public override String GetFunctionDeclareScript()
        {
            String xslExpression = _xmlNode.Attributes["test"].Value;
            String jsExpression = ExpressionParser.ConvertToJSExpression(xslExpression);
            String result = "function " + _jsFunctionName + "_" + _index.ToString() + "() { \r\n";
            result += "if (" + jsExpression + ")\r\n";
            result += "{\r\nvar result = false;";
            foreach (XslElement node in _xslChildNodes)
            {
                result += "\r\nresult += " + node.GetFunctionCallingScript();
            }
            result += "\r\nreturn result;\r\n}\r\n";
            result += "return false;\r\n}\r\n";
            return result;
        }
    }

}
